﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WSNs
{
    public partial class Form_AddNode : Form
    {

        public int Fathernode { get; set; }
        public int Childnode { get; set; }
        //定义事件 
        public Action<CtlNode> ConnectAddnode;
        private CtlNode _config = new CtlNode();
        public Form_AddNode(NodesType nt)
        {
            InitializeComponent();
            switch (nt)
            {
                case NodesType.EndNode:
                    cb_end.Checked = true;
                    _config.NodeType = NodesType.EndNode;
                    break;
                case NodesType.Router:
                    cb_router.Checked = true;
                    _config.NodeType = NodesType.Router;
                    break;
            }
        }

        private void Form_AddNode_Load(object sender, EventArgs e)
        {
            tb_child.Focus();
            checkBox_continue.Checked = true;
            cb_router.Enabled = false;
            cb_end.Enabled = false;
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                _config.FatherNode = Convert.ToByte(tb_father.Text);
                _config.NodeID = Convert.ToByte(tb_child.Text);
                AcqStructure acq = new AcqStructure();
                acq.AcqMode = AcqMode.Freedom;
                acq.NodeID = (byte)_config.NodeID;
                acq.FileID = 1;
                acq.CoupleMode = CoupleMode.AC;
                acq.Frequency = 51200;
                acq.AcqLength = 0;
                acq.MeasuerRange = 0;
                acq.TrigerV = 0;
                acq.StorageLocation = StorageLocation.MicroSD;
                _config.AcqStructure = acq;
                //触发connectWSN事件
                ConnectAddnode(_config);
                tb_child.Focus();
                tb_child.SelectAll();
            }
            catch
            {
                MessageBox.Show("添加节点无效");
                return;
            }
            if (!checkBox_continue.Checked)
            {
                this.Close();
            }
        }
    }
}
